<?php

namespace Sharks\Core\Http\View;

use Sharks\Core\AppContext;
use Sharks\Core\Module;
use Sharks\Core\Support\Base\Argument;
use Sharks\Core\Support\Smarty\SmartyVariableProvider;

/**
 * Smarty 视图表示
 */
class SmartyView extends AbstractSmartyView {
    const DEFAULT_VIEW_SUFFIX = 'vue.php';

    const TYPE_PUBLIC = 1;
    const TYPE_CONTROL = 2;

    private $module;
    private $type;
    private $view;
    private $displayPath;

    public function __construct($type, $view, $data = null, $module = null) {
        $this->module = Module::get($module);
        $this->type = $type;
        $this->view = $view;
        parent::__construct($data);
    }

    /**
     * 获得完整的view文件路径
     */
    public function getViewPath() {
        $app = AppContext::instance();
        $viewsDir = is_null($this->module) ? $app->path()->app->source . '/Views' : $this->module->path()->view;

        $viewsDir .= self::TYPE_PUBLIC == $this->type ? '/Public' : '/Control';

        // 处理移动设备
        if ($app->request()->browserDetect->isMobile() && is_dir($viewsDir . '/Mobile')) {
            $viewsDir = $viewsDir . '/Mobile';
        }
        elseif (is_dir($viewsDir . '/Default')) {
            $viewsDir = $viewsDir . '/Default';
        }

        return $viewsDir . '/' . $this->view . '.' . self::DEFAULT_VIEW_SUFFIX;
    }

    /**
     * 渲染前
     */
    public function beforeRender() {
        $provider = new SmartyVariableProvider();
        $provider->handle($this->smarty);
    }
}